<?php

namespace Biz\service\Userpenalty;

use Doraemon\cache\BlackList;
use Doraemon\model\AdmUserType;
use Doraemon\service\admin\AdmUserFilter;
use Doraemon\service\CommonEmblem;
use Doraemon\service\search\AdmUserSearch;
use Doraemon\service\User;
use Doraemon\model\BestUser;
use Doraemon\model\AdmRecommendUser;
use Doraemon\model\Member;
use Doraemon\cache\Queue;
use Doraemon\cache\SolrUpdateCollector;
use Doraemon\cache\EsUpdateCollector;
use Doraemon\service\solr\SolrUserMark;
use Doraemon\model\AdmToBeMarked;
use Doraemon\model\AdmOperateLog;

class OperateLogo
{
    const AUTH_TAG = [
        AdmUserType::AUTH_TAG_NO => '无',
        AdmUserType::AUTH_TAG_SUSPECT_BUSINESS => '疑似商业用户',
        AdmUserType::AUTH_TAG_TO_BRAND => '待认证品牌',
    ];
    const QUALITY = [
        AdmUserType::NORMAL => '未标识状态',
        AdmUserType::B3 => 'B3(普通网图+运营灰名单)',
        AdmUserType::B2 => 'B2(商业灰名单)',
        AdmUserType::B1 => 'B1(黑名单)',
        AdmUserType::XL => 'XL'
    ];

    public static $user_quality = ['good' => 1, 'normal' => 2, 'S2' => 3, 'B2' => 9, 'B1' => 10, 'B3' => 11];

    public function handle($data = [])
    {
        $uid = $data['uid'];
        if(empty($uid)) return false;
        $quality = $data['quality'];
        $admin_id = $data['admin_id'];
        $admin_name = $data['admin_name'];
        //获取运营级用户质量
        $qualityOld = (int)self::getUserQuality($uid);
        //获取已存在的标签信息
        $update_data = [
            'quality' => $quality,
            'addtime' => time(),
            'admin_id'=> $admin_id,
        ];
        //处理优质用户的相关逻辑
        if (!in_array($qualityOld, [false, 3])){
            if ($qualityOld != $quality){
                // 更新|删除老的用户质量数据
                if ($qualityOld == self::$user_quality['S2']){
                    $judge = self::delBestUser($uid);
                    if ($judge) {
                        $rs = AdmUserType::updateInfo($uid,$update_data);
                    }
                }elseif (in_array($qualityOld, [self::$user_quality['B1'], self::$user_quality['B2'], self::$user_quality['B3'], self::$user_quality['normal'], self::$user_quality['good']])){
                    $rs = AdmUserType::updateInfo($uid,$update_data);
                }
                if (!$rs) {
                    return  false;
                }
            }else{
                $rs = AdmUserType::updateInfo($uid,$update_data);
            }
        }else{
            $rs = AdmUserType::getByUid($uid);
            if ($rs){
                $rs = AdmUserType::updateInfo($uid,$update_data);
            }else{
                $user_addtime = Member::getOne($uid)['addtime'];
                $update_data['addtime'] = $user_addtime;
                $rs = AdmUserType::save($uid,$update_data);
            }
        }

        Queue::insertQueue('Photo/reindex_photo_solr', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        SolrUpdateCollector::insert($uid);
        EsUpdateCollector::insert($uid);
        if (empty($rs)){
            return false;
        }else{
            if ($quality && $quality == self::$user_quality['B1']){
                BlackList::addUid2BlackList($uid);
                //B1用户内容更新 事件化实现
//                Queue::insertQueue('UserB1/setStatusByUid', array(
//                    'uid' => $uid,
//                    "type"=>"set_b1"
//                ), Queue::QUEUE_COMMON);
            }

            if (($quality && $quality != self::$user_quality['B1']) || $quality == AdmUserType::NORMAL){
                BlackList::delUid4BlackList($uid);
                //取消B1内容更新 事件化实现
//                Queue::insertQueue('UserB1/setStatusByUid', array(
//                    'uid' => $uid,
//                    "type"=>"cancel_b1"
//                ), Queue::QUEUE_COMMON);
            }

            if ($quality && $quality == self::$user_quality['B2']) {
                BlackList::addUid2B2List($uid);
            }

            if (($quality && $quality != self::$user_quality['B2']) || $quality == AdmUserType::NORMAL){
                BlackList::delUid4B2List($uid);
            }
        }
        if ( $quality ) {
            AdmToBeMarked::deleteByUid($uid);
        }
        AdmUserFilter::updateUserQuality($uid);
        AdmUserSearch::updateAdmUserSearchByUid($uid);
        AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['edit_user_quality'],$admin_id,$admin_name,$qualityOld,$quality);
        return true;
    }

    //获取运营标识信息
    private static function getUserQuality($uid){
        if (BestUser::checkBest($uid)){
            return self::$user_quality['S2'];
        }
        //是否在灰(B2)，黑名单(B1)里
        $qualityOld = AdmUserType::getByUid($uid);
        if ($qualityOld) {
            return $qualityOld['quality'];
        } else {
            return false;
        }
    }

    //移除用户S2信息
    private static function delBestUser($uid){
        // 删除用户在推荐表的数据
        AdmRecommendUser::deleteByUid($uid);
        // 删除精华表数据
        BestUser::deleteUser($uid);
        return true;
    }

}
